package jjn.round1;

import org.junit.platform.commons.util.StringUtils;

import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/7/29 14:48
 */
public class LeetCode36 {
    public boolean isValidSudoku(char[][] board) {
        boolean[][] rows = new boolean[board.length][board.length];
        boolean[][] columns = new boolean[board.length][board.length];
        boolean[][] subBoards = new boolean[board.length][board.length];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] != '.') {
                    int num = board[i][j] - '1';
                    int subBoardIndex = i / 3 * 3 + j / 3;
                    if (rows[i][num] || columns[j][num] || subBoards[subBoardIndex][num]) {
                        return false;
                    }
                    rows[i][num] = true;
                    columns[j][num] = true;
                    subBoards[subBoardIndex][num] = true;
                }
            }
        }
        return true;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        String s = scanner.nextLine();
        char[][] board = new char[count][count];
        for (int i = 0; i < count; i++) {
            String line = scanner.nextLine();
            if (StringUtils.isBlank(line)) {
                continue;
            }
            String[] parts = line.split(" ");
            for (int j = 0; j < parts.length; j++) {
                String part = parts[j];
                if (part == null || part.isBlank()) {
                    continue;
                }
                board[i][j] = part.charAt(0);
            }
        }
        boolean validSudoku = new LeetCode36().isValidSudoku(board);
        System.out.println(validSudoku);
    }
}
